<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\Modular;
use app\admin\model\UpFile;
use think\Request;

class Api
{
    /**设置模块
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function set_modular(Request $request){
        $data = $request->param();
        $modular = config('modular.');
        foreach ($modular as $value){
            if($value['dir']==$data['dir']){
                echo '此模块已存在';
                exit;
            }
        }
        $modular = array_merge($modular,[$data]);
        if(Modular::changeConfig('',$modular)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**后台上传图片接口
     * @return \think\response\Json
     */
    public function upimg(){
        $data = UpFile::upfiles();
//        return $data;
        if($data){
            return json_to_respos('上传成功',200,$data);
        }else{
            return json_to_respos('上传失败',205);
        }
    }

    /**删除图片接口
     * @param Request $request
     * @return \think\response\Json
     */
    public function deleteimg(Request $request){
        $id = $request->param('id');
        if(UpFile::delete($id)){
            return json_to_respos('删除成功');
        }else{
            return json_to_respos('删除失败',204);
        }
    }

    /**编辑框编辑的图片上传
     * @param Request $request
     * @return \think\response\Json
     */
    public function editorimgs(Request $request){
        $datas = UpFile::upfiles();
        $data['code'] = 0;
        $data['msg'] = '上传成功';
        $data['data'] = [
            "src"=>$datas[0]['path']
            ,"title"=>$datas[0]['yuan_name']
        ];
        return json($data);
    }

    /**分片上传图片
     * @param Request $request
     * @return \think\response\Json
     */
    public function fenpian(Request $request){
//        $data = $request->param();
        $data = UpFile::FenPian();
        return json($data);
    }
}